package controller

import (
	"github.com/mitchellh/mapstructure"
	"log"
	"mssgserver/constant"
	"mssgserver/net"
	"mssgserver/server/game/gameConfig"
	"mssgserver/server/game/model"
)

var DefaultNationMapController = &nationMapController{}

type nationMapController struct {
}

func (n *nationMapController) Router(router *net.Router) {
	group := router.Group("nationMap")
	group.AddRouter("config", n.config)
}

/*
*
加载地图配置资源
*/
func (n *nationMapController) config(req *net.WsMsgReq, rsp *net.WsMsgRsp) {
	reqObj := &model.ConfigReq{}
	rspObj := &model.ConfigRsp{}
	rsp.Body.Seq = req.Body.Seq
	rsp.Body.Name = req.Body.Name
	err := mapstructure.Decode(req, reqObj)
	if err != nil {
		log.Println("nationMap.config参数转换失败")
		rsp.Body.Code = constant.InvalidParam
		return
	}
	cfg := gameConfig.MapBuildConf.Cfg
	rspObj.Confs = make([]model.Conf, len(cfg))
	for index, conf := range cfg {
		rspObj.Confs[index].Name = conf.Name
		rspObj.Confs[index].Stone = conf.Stone
		rspObj.Confs[index].Iron = conf.Iron
		rspObj.Confs[index].Wood = conf.Wood
		rspObj.Confs[index].Grain = conf.Grain
		rspObj.Confs[index].Defender = conf.Defender
		rspObj.Confs[index].Durable = conf.Durable
		rspObj.Confs[index].Type = conf.Type
		rspObj.Confs[index].Level = conf.Level
	}
	rsp.Body.Code = constant.OK
	rsp.Body.Msg = rspObj
}
